package indi.jp.project.core.service.impl;

import indi.jp.project.core.dao.BaseDao;
import indi.jp.project.core.entity.JsonResult;
import indi.jp.project.core.entity.Role;
import indi.jp.project.core.entity.RoleUser;
import indi.jp.project.core.service.RoleService;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RoleServiceImpl implements RoleService {

    private BaseDao baseDao;

    public RoleServiceImpl(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public Map<String, Object> queryGrid(Role entity) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        List<Role> list = baseDao.queryList("roleMapper.queryList", entity);
        int count = baseDao.queryCount("roleMapper.queryCount", entity);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public JsonResult save(Role entity) throws SQLException {
        if ("".equals(entity.getId())) {
            baseDao.insert("roleMapper.save", entity);
        } else {
            baseDao.insert("roleMapper.edit", entity);
        }
        return JsonResult.success("");
    }

    @Override
    public int deleteById(String id) throws SQLException {
        return baseDao.deleteById("roleMapper.deleteById", id);
    }

    @Override
    public Map<String, Object> queryRoleUserGrid(RoleUser entity) throws SQLException {
        Map<String, Object> map = new HashMap<>();
        List<RoleUser> list = baseDao.queryList("roleMapper.queryRoleUserList", entity);
        int count = baseDao.queryCount("roleMapper.queryRoleUserCount", entity);
        map.put("grid", list);
        map.put("total", count);
        return map;
    }

    @Override
    public JsonResult addRoleUser(RoleUser entity) {
        try {
            int num = baseDao.queryCount("roleMapper.queryRepeatRoleUser", entity);
            if(num > 0){
                return JsonResult.failure("该角色已包含当前用户");
            }
            int effectRow = baseDao.insert("roleMapper.addRoleUser", entity);
            if (effectRow > 0) {
                return JsonResult.success("");
            } else {
                return JsonResult.failure("移入失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public JsonResult removeRoleUser(String id) {
        try {
            int effectRow = baseDao.deleteById("roleMapper.removeRoleUser", id);
            if (effectRow > 0) {
                return JsonResult.success("");
            } else {
                return JsonResult.failure("未能成功移除，请刷新查看数据是否存在");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return JsonResult.error();
        }
    }

    @Override
    public List<Role> queryRolesByUsername(String username) throws SQLException {
        return baseDao.queryList("roleMapper.queryRolesByUsername", username);
    }


}
